FROM ubuntu:20.04 as sgxbase

RUN apt-get update && apt-get install -y \
    gnupg \
    wget \
    curl 


ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

#RUN echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu bionic main' > /etc/apt/sources.list.d/intel-sgx.list
RUN echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu focal main' | tee /etc/apt/sources.list.d/intel-sgx.list
RUN wget -qO - https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | apt-key add -
RUN apt-get update 

FROM sgxbase as aesm 

ARG DEBIAN_FRONTEND=noninteractive

#RUN echo "Y" | apt install systemd
#先设置时区
RUN export TZ=Asia/Shanghai
RUN apt-get install -y \
    libcurl4 \
    libssl1.1 \
    make \
    cracklib-runtime \
    pkg-config \
    lsb-release \
    ca-certificates \
    gnupg \
    binutils \
    vim \
    libpcre3 \
    libpcre3-dev \
    zlib1g-dev 

RUN mkdir -p /usr/local/rongan/tee/bin && mkdir -p /usr/local/rongan/tee/conf


#nodejs 16    
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt-get update && \
    apt-get install nodejs -y
#RUN apt-get install sgx-dcap-pccs

#SGXSDK
ARG sgx_sdkbin=sgx_linux_x64_sdk_2.21.100.1.bin
WORKDIR /opt/intel
RUN wget https://download.01.org/intel-sgx/latest/linux-latest/distro/ubuntu20.04-server/$sgx_sdkbin
RUN chmod a+x $sgx_sdkbin
RUN echo "yes" | ./$sgx_sdkbin
RUN rm $sgx_sdkbin
RUN bash /opt/intel/sgxsdk/environment 


#SGX lib
RUN apt-get update -y && \
    echo "Y"|apt-get install libsgx-epid \
    libsgx-quote-ex \
    libsgx-dcap-ql \
    libsgx-urts \
    libsgx-launch \
    libsgx-enclave-common

RUN apt-get install -y libsgx-enclave-common-dev \
    libsgx-dcap-ql-dev \
    libsgx-dcap-default-qpl-dev \
    libsgx-dcap-quote-verify-dev*

RUN apt install cpuid


#gramine
RUN curl -fsSLo /usr/share/keyrings/gramine-keyring.gpg https://packages.gramineproject.io/gramine-keyring.gpg
RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/gramine-keyring.gpg] https://packages.gramineproject.io/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/gramine.list
RUN apt-get update && echo "Y"| apt install gramine
RUN gramine-sgx-gen-private-key 

#安装pccs
RUN mkdir /etc/init

RUN apt-get -y --no-install-recommends  install sgx-dcap-pccs
ADD ./conf/ssl_key.tar.gz /opt/intel/sgx-dcap-pccs/
WORKDIR /opt/intel/sgx-dcap-pccs

RUN npm config set registry https://registry.npm.taobao.org && \
    npm install

#安装 sgx-pck-id-retrieval-tool
RUN apt install -y sgx-pck-id-retrieval-tool


RUN mkdir -p /usr/local/rongan/tee/conf && \
    mkdir -p /usr/local/rongan/tee/bin && \
    mkdir -p /usr/local/rongan/tee/packages \
    mkdir -p /usr/local/rongan/tee/service

#拷贝示例
#ADD ./example/CI-Examples.tar.gz /usr/local/rongan/tee/bin
ADD ./example/ra-tls-mbedtls.tar.gz /usr/local/rongan/tee/bin
ADD ./example/RemoteAttestationTest.tar.gz /usr/local/rongan/tee/bin
ADD ./example/r.tar /usr/local/rongan/tee/bin

#代理程序
ADD ./modules/lib_ra_base_log.tar.gz /usr/local/rongan/tee/packages
ADD ./modules/ra_local_tcpws.tar.gz /rongan

#日志库
WORKDIR /usr/local/rongan/tee/packages/lib_ra_base_log
RUN bash install.sh

#python微服务
ADD ./modules/py-http.tar.gz /usr/local/rongan/tee/service

#安装PCCS管理员工具
ADD ./modules/PccsAdminTool.tar.gz /usr/local/rongan/tee/bin
WORKDIR /usr/local/rongan/tee/bin/PccsAdminTool
RUN apt update && \
    apt install python3-pip -y && \
    pip3 install keyring keyrings.alt
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

#grpc
ADD ./modules/mpc_grpc_server.tar.gz /usr/local/rongan/tee/service
WORKDIR /usr/local/rongan/tee/service/mpc_grpc_server
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/


#代理程序
WORKDIR  /rongan/ra_local_tcpws
RUN bash install.sh

RUN pip3 install flask gevent -i https://mirrors.aliyun.com/pypi/simple/

#启动脚本
COPY ./main.sh /main.sh
COPY ./init_proxy.sh /usr/local/rongan/bin/service/init_proxy.sh
COPY ./start_tcpws_mpc.sh /usr/local/rongan/bin/service/start_tcpws_mpc.sh

# More aesm plugins, e.g libsgx-aesm-quote-ex-plugin, are needed if application requires attestation. See installation guide.
RUN apt-get install -y libsgx-aesm-launch-plugin

#修改默认的配置文件
#RUN sed -i 's/"use_secure_cert": true/"use_secure_cert": false/' /etc/sgx_default_qcnl.conf
#ADD ./conf/default.json /opt/intel/sgx-dcap-pccs/config/default.json

WORKDIR /usr/local/rongan/tee
#通过脚本启动服务
CMD bash /main.sh
